package nl.RvdW.TVgids;

import java.util.ArrayList;
import java.util.Timer;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

public class TimeFragment extends ListFragment {
	private TimeAdapter lv1_content;
	private Timer t;
	private TimeFragmentTask ttask;
	private ArrayList<Programme> programs;
	
	public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        programs = new ArrayList<Programme>();
		lv1_content = new TimeAdapter(getActivity(), android.R.layout.simple_list_item_1,
				programs);
		setListAdapter(lv1_content);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		Programme p = Global.db.getProgramme(v.getId());
		if(p!=null){
			Global.showProgramme(p);
		}
	}
	
	@Override
	public void onResume() {
		super.onResume();

		ttask = new TimeFragmentTask(programs, getArguments().getBoolean("current",true));
		ttask.run();
		t=new Timer();
		try {
			t.scheduleAtFixedRate(ttask, 61000-(System.currentTimeMillis()%60000), 60000);
		} catch (Exception e) {
			Log.i(Global.Name,"Resume error: schedule timer!");
		}
	}
	
	@Override
	public void onPause() {
		super.onPause();
		t.cancel();
		t.purge();
	}
}